package com.zxr.system.service.impl;

import java.util.Date;
import java.util.List;
import com.zxr.common.utils.DateUtils;
import com.zxr.system.domain.MemberProduct;
import com.zxr.system.domain.Products;
import com.zxr.system.mapper.ProductsMapper;
import com.zxr.system.service.IMemberProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zxr.system.mapper.MemberMapper;
import com.zxr.system.domain.Member;
import com.zxr.system.service.IMemberService;
import com.zxr.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员Service业务层处理
 * 
 * @author zxrproject
 * @date 2025-04-26
 */
@Service
public class MemberServiceImpl implements IMemberService 
{
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private IMemberProductService memberProductService;


    /**
     * 查询会员
     * 
     * @param id 会员主键
     * @return 会员
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper.selectMemberById(id);
    }

    public Member selectMemberByOpenId(String openId){
        return memberMapper.selectMemberByOpenId(openId);
    }

    /**
     * 查询会员列表
     * 
     * @param member 会员
     * @return 会员
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增会员
     * 
     * @param member 会员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMember(Member member)
    {
        member.setCreateTime(DateUtils.getNowDate());
        //插入用户
        memberMapper.insertMember(member);
        //关联商品
        List<Products> list = productsMapper.selectProductsList(null);
        for(int i=0;i<list.size();i++){
            MemberProduct memberProduct =new MemberProduct();
            memberProduct.setMemberId(member.getId());
            memberProduct.setProductId(list.get(i).getProductId());
            memberProduct.setmPrice(list.get(i).getPrice());
            memberProduct.setCreateTime(new Date());
            memberProductService.insertMemberProduct(memberProduct);
        }
        return 1;
    }

    /**
     * 修改会员
     * 
     * @param member 会员
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除会员
     * 
     * @param ids 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(String ids)
    {

        return memberMapper.deleteMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员信息
     * 
     * @param id 会员主键
     * @return 结果
     */
    @Transactional
    public int deleteMemberById(Long id)
    {
        memberProductService.deleteMemberProductByMemberId(id);
        return memberMapper.deleteMemberById(id);
    }
}
